package com.visikard.asynctask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.TransactionHistoryBUS;
import com.visikard.response.BaseResponse;
import com.visikard.response.TransactionHistoryRewardPunchKardResponse;

public class GetTransactionHistoryRewardPunchKardTask extends BaseTask {

	public static final int POS_UID = 0;
	public static final int POS_PAGE_INDEX = 1;
	public static final int POS_PAGE_SIZE = 2;
	public static final String KEY_NB_REWARDED_GIFTKARD = "key_nb_rewarded_giftkard";
	public static final String KEY_NB_REDEEMED_GIFTKARD = "key_nb_redeemed_giftkard";

	public GetTransactionHistoryRewardPunchKardTask(Handler handler) {
		super(handler);
	}

	public GetTransactionHistoryRewardPunchKardTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {

		// Get params
		long uid = (Long) params[POS_UID];
		int pageIndex = (Integer) params[POS_PAGE_INDEX];
		int pageSize = (Integer) params[POS_PAGE_SIZE];

		// Validation

		// Call service
		TransactionHistoryBUS busObj = new TransactionHistoryBUS();
		return busObj.getTransactionHistoryRewardedPunchKard(uid, pageIndex, pageSize);
	}

	@Override
	protected boolean handleResult(Object result) {
		TransactionHistoryRewardPunchKardResponse response = (TransactionHistoryRewardPunchKardResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					Message successMsg = new Message();
					successMsg.what = UIMessage.MSG_GET_LIST_SUCCESS;
					successMsg.obj = response.getListTransactionRewardPunchKards();
					Bundle data = new Bundle();
					data.putInt(KEY_NB_REWARDED_GIFTKARD, response.getNbGiftKardRewarded());
					data.putInt(KEY_NB_REDEEMED_GIFTKARD, response.getNbGiftKardRedeemed());
					successMsg.setData(data);
					mUIHandler.sendMessage(successMsg);
					return true;
			}
		}
		return false;
	}

}
